package pers.liaohaolong.mapzoner;

import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

@Slf4j
public class Main {

    public static void main(String[] args) throws IOException {
        log.info("地图分区器");
        Scanner scanner = new Scanner(System.in);
        log.info("请输入图片路径：");
        String inputPath = scanner.nextLine();
        log.info("请输入输出图片路径：");
        String outputPath = scanner.nextLine();

        log.info("读取图片...");
        BufferedImage image = ImageIO.read(new File(inputPath));

        MapZoner mapZoner = new MapZoner();
        mapZoner.setBackgroundColorRgb(Color.WHITE.getRGB());
        if (!mapZoner.colorCheck(image)) {
            return;
        }
        log.info("颜色列表：");
        for (int color : mapZoner.getColors()) {
            if (color == mapZoner.getBackgroundColorRgb())
                log.info("* {} 背景色", String.format("0x%06X", color));
            else
                log.info("* {}", String.format("0x%06X", color));
        }
        // 生成确认
        log.info("是否确认生成？(Yy/Nn)");
        if (!scanner.nextLine().equalsIgnoreCase("y")) {
            log.info("取消生成");
            return;
        }
        mapZoner.generateBoundaries(image);
        // 保存设置
        log.info("保存时，是否使用原图作为背景？(Yy/Nn)");
        if (scanner.nextLine().equalsIgnoreCase("y"))
            SaveUtils.save(image, mapZoner.getBoundaries(), Color.BLACK.getRGB(), outputPath);
        else
            SaveUtils.save(mapZoner.getBackgroundColorRgb(), mapZoner.getBoundaries(), Color.BLACK.getRGB(), outputPath);
    }

}
